package com.freeBox;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class Register {

    @Autowired
    UserMapper userMapper ;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/register")
    public String userRegisterPage() {
        return "register";
    }


    @PostMapping("/register")
    public String UserRegister(@RequestParam("id") String id ,
                               @RequestParam("name") String name,
                               @RequestParam("pwd") String pwd ,
                               @RequestParam("sex") String sex ,
                               @RequestParam("tel") String tel ,
                               @RequestParam("email") String email ,
                               @RequestParam("profession") String profession ,
                               @RequestParam("yzm") String yzm,
                               HttpServletRequest request ,
                               Model model)  {
        HttpSession session = request.getSession() ;
        String req_yzm = session.getAttribute("yzm").toString() ;
        if(yzm.equals(req_yzm)) {
            try {
                if(!getUser(id)) {
                    if(addUser(id , pwd)) {
                        userMapper.addUser(id , name , pwd , sex , profession , tel , email);
                        request.setAttribute("userlogin" , id);
                        return "platform";
                    } else {

                        return "erro";
                    }

                } else {
                    model.addAttribute("msg", "账号已存在");
                    return "register" ;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "register" ;
         }
        }
        model.addAttribute("msg" , "验证码错误");
        return "register";
    }



    public boolean getUser(String username) {
        try {
            ValueOperations<String , String> operations = redisTemplate.opsForValue() ;
            String name = operations.get(username);
            if(name.isEmpty())
                return true;
            else
                return false;
        } catch (Exception e) {
            return false;
        }
    }
    public boolean addUser(String id , String pwd) {
        try {
            ValueOperations<String , String> operations = redisTemplate.opsForValue() ;
            operations.set(id , pwd);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public String getUserinfo() {
        return "AlluserInfo" ;
    }

}
